package com.xinzhi.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinzhi.anno.CheckRole;
import com.xinzhi.domain.User;
import com.xinzhi.domain.vo.TopicCondition;
import com.xinzhi.service.LoginService;
import com.xinzhi.service.UserService;
import com.xinzhi.service.impl.TopicServiceImpl;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("/mtyx-admin/member")
public class AdminUserController {
    @Autowired
    TopicServiceImpl topicService;
    @Autowired
    UserService userService;
    @Autowired
    private LoginService loginService;
    @CheckRole(role = {"admin"})
    @RequestMapping("/memberpagelist")
    public Result pageAll(TopicCondition con){
        if (con.getPageNo() == null) {
            con.setPageNo(1);
        }
        if (con.getPageSize() == null) {
            con.setPageSize(2);
        }
        int count = userService.list().size() % con.getPageSize() == 0 ?
                userService.list().size() / con.getPageSize() : userService.list().size() / con.getPageSize();
        if (con.getPageNo() > count) {
            con.setPageNo(count);
        }
        List<User> page = ListUtil.page(con.getPageNo() - 1, con.getPageSize(), userService.list());
        return Result.ok(page);
    }
    @CheckRole(role = {"admin"})
    @RequestMapping("/memberup")
    public Result upUser(User user){
        if (user != null){
            if (userService.updateById(user)){
                return Result.ok(user.getTname());
            }
            return Result.error(5003,"系统出错");
        }
        return Result.error(5002,"输入不能为空");
    }
    @CheckRole(role = {"admin"})
    @RequestMapping("/membersave")
    public Result inUser(User user){
        if (user != null){
                return userService.saveUser(user);
        }
        return Result.error(5002,"选择不能为空");
    }
    @CheckRole(role = {"admin"})
    @RequestMapping("/memberremove")
    public Result removeUser(User user){
        if (user != null){
            user.setRemove(1);
            if (userService.updateById(user)) {
                return Result.ok("停用");
            }
            return Result.error(5003,"系统错误");
        }
        return Result.error(5002,"不能为空");
    }
}
